package LeetCodeP;
import java.util.Stack;
public class Soluction1021 {

    public static String removeOuterParentheses(String S) {

        char[] temp = S.toCharArray();
        char[] result = new char[temp.length+3*2];
        int j = 0;
        int z=0;
        int x=0;
        for(int i = 0;i<temp.length;i++,j++){

          /*  if (temp[i]=='('&&temp[i+1]==')'){

                System.out.println("temp[i]:"+temp[i]);
                System.out.println("temp[i+1]:"+temp[i+1]);
                result[j++]=temp[i];
                result[j]=temp[i+1];


            }*/

        }
        System.out.println(String.valueOf(result));
        return String.valueOf(result);




    }

    public static String removeOuterParentheses1(String S) {


        char[] charArr = S.toCharArray();
        StringBuilder sb = new StringBuilder();
        int p_l = 0;
        int p_r = 0;
        for (int i = 0; i < charArr.length; i++) {
            if ('(' == charArr[i]) {
                p_l++;
            }
            if (')' == charArr[i]) {
                p_r++;
            }
            //找到原语
            if (p_l == p_r) {
                sb.append(S.substring(i - p_l * 2 + 2, i));
                p_l = 0;
                p_r = 0;
            }
        }
        System.out.println(sb.toString());
        return sb.toString();

    }

    public String removeOuterParentheses2(String S) {
        StringBuilder ans = new StringBuilder();
        Stack<Character> stack = new Stack<>();

        int start = 0;// 初始化原语的起始位置
        int end = 0;// 初始化原语的结束位置
        boolean flag = false;// 标志每个原语

        for (int i = 0;i < S.length();i++) {
            char ch = S.charAt(i);

            if (ch == '(') {// 遇到左括号，入栈
                stack.push(ch);
                if (!flag) {// 遇到的第一个左括号，是原语的开始位置，记录下原语开始位置
                    start = i;
                    flag = true;
                }
            }

            if (ch == ')') {// 遇到右括号，出栈
                stack.pop();
                if (stack.isEmpty()) {// 当栈空的时候，找到了一个完整的原语
                    end = i;// 记录下结束位置
                    ans.append(S.substring(start + 1,end));// 去掉原语的最外层括号，并追加到答案中
                    flag = false;// 置标志为false，往后接着找下一个原语
                    start = end;// 往后找，再次初始化原语开始位置
                }
            }
        }

        return ans.toString();
    }


    public static String removeOuterParentheses4(String S) {
        Stack<Character> stack = new Stack<>();
        StringBuilder sb = new StringBuilder();
        int start = 0;
        for(int i = 0; i < S.length(); i++){
            if(S.charAt(i) == '('){
                stack.push('(');
            }else{
                stack.pop();
                if(stack.isEmpty()){
                    sb.append(S.substring(start + 1, i));
                    start = i + 1;
                }
            }
        }
        System.out.println(sb.toString());
        return sb.toString();
    }




    public static void main(String[] args) {
        String S="(()())(())(()(()))";
        removeOuterParentheses4(S);

    }
}
